package com.woniu.cq.medical.controller;


import com.woniu.cq.medical.common.Result;
import com.woniu.cq.medical.controller.form.AttendanceRecordForm;
import com.woniu.cq.medical.controller.form.PatientWaitForm;
import com.woniu.cq.medical.controller.form.VirtualCallMessageForm;
import com.woniu.cq.medical.entity.Doctor;
import com.woniu.cq.medical.controller.form.DoctorOrDutyForm;
import com.woniu.cq.medical.entity.Patient;
import com.woniu.cq.medical.service.DoctorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-09-05
 */
@RestController
@RequestMapping("/medical/doctor")
@Slf4j
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @GetMapping("getDiagnosisSchedule")
    public Result showDiagnosisSchedule(Date date){//诊疗进度
        List<AttendanceRecordForm> formList = doctorService.showDiagnosisSchedule(date);
        return Result.success(formList);
    }
    @PostMapping("login")
    public Result doctorLogin(int doctorId, String doctorPassword, HttpSession session){//医生登陆
//        Subject currDoctor = SecurityUtils.getSubject();
//        try {
//            currDoctor.login(new UsernamePasswordToken(doctorId,doctorPassword));
//            return Result.success();
//        }catch (AuthenticationException e){
//            log.info("医生：{} 登陆失败,用户名或密码错误",username);
//            return Result.fail("AUTH_ERROR","用户名或密码错误");
//        }
        Doctor doctor = doctorService.doctorLogin(doctorId, doctorPassword);
        session.setAttribute("doctor",doctor);
        return Result.success();
    }

    @GetMapping("selectAdministrationOrDoctorList")
    public Result selectAdministrationOrDoctorList(Integer administrativeId) throws ParseException {
        List<DoctorOrDutyForm> doctorOrDutyForms = doctorService.queryAdministrationOrDoctorList(administrativeId);
        return Result.success(doctorOrDutyForms);
    }
    @GetMapping("messageInfo")
    public Result getDoctorInfo(HttpSession session){
        Doctor doctor=(Doctor) session.getAttribute("doctor");//当前医生信息
        if(doctor==null){
            return Result.fail("1001","未登录，请先登陆");
        }
        VirtualCallMessageForm virtualCallMessageForm=doctorService.getVirtualCallMessageForm(doctor.getDoctorId());
        virtualCallMessageForm.setDoctor(doctor);
        return Result.success(virtualCallMessageForm);
    }

    @GetMapping("called")
    public Result callPatientByPatientId(PatientWaitForm patientWaitForm){
        String patientName = patientWaitForm.getPatientName();
        return Result.success();
    }
    @GetMapping("treatment")
    public Result treatmentByParams(int doctorId,String currPatient,int registerId,HttpSession session){
        Doctor doctor=(Doctor) session.getAttribute("doctor");//当前医生信息
        String status="就诊中";
        Result result=doctorService.doctorTreatmentService(doctorId,status,currPatient,registerId,doctor);
        return result;
    }
    @GetMapping("diagnoseFinish")
    public Result diagnoseFinishByParams(int doctorId,int registerId,HttpSession session){
        Doctor doctor=(Doctor) session.getAttribute("doctor");//当前医生信息
        String status="已就诊";
        String currPatient="无";
        Result result=doctorService.doctorDiagnoseFinishService(registerId,status,doctorId,currPatient,doctor);
        return result;
    }
    @GetMapping("passPatient")
    public Result passPatient(int doctorId,int registerId,HttpSession session){
        Doctor doctor=(Doctor) session.getAttribute("doctor");//当前医生信息
        String status="就诊中";
        Result result=doctorService.doctorPassPatientService(doctorId,status,registerId,doctor);
        return result;
    }


    @GetMapping("selectDoctorList")
    public Result selectDoctorList(Integer administrativeId){
        List<Doctor> doctors = doctorService.queryDoctorList(administrativeId);
        return Result.success(doctors);
    }
    @PostMapping("Login")
    public Result userLogin(int doctorId, String doctorPassword, HttpSession session){//医生登陆
        Subject currDoctor = SecurityUtils.getSubject();
        try {
            currDoctor.login(new UsernamePasswordToken(doctorId+"",doctorPassword));
            Doctor doctor = doctorService.userLogin(doctorId);
            session.setAttribute("doctor",doctor);
            currDoctor.hasRole("doctor");
            System.out.println(doctor.getDoctorName()+"是否拥有doctor角色？"+currDoctor.hasRole("doctor"));
            return Result.success();
        }catch (AuthenticationException e){
            log.info("医生：{} 登陆失败,用户名或密码错误",doctorId);
            return Result.fail("AUTH_ERROR","用户名或密码错误");
        }
    }
    @PostMapping("addAttendanceRecord")
    @RequiresRoles({"doctor"})
    public Result addAttendanceRecord(HttpSession session){
        Doctor doctor=(Doctor)session.getAttribute("doctor");
        return doctorService.addAttendanceRecord(doctor);
    }
}

